package com.alpaak.common.utils.log;

import com.alpaak.common.utils.DateUtil;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;

public class LogMsg {
	private String dateTime;
	private int code;
	private String location;
	private String message;
	private Throwable exception;
	
	public String getDateTime() {
		return dateTime;
	}
	public void setDateTime(String dateTime) {
		this.dateTime = dateTime;
	}
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Throwable getException() {
		return exception;
	}
	public void setException(Throwable exception) {
		this.exception = exception;
	}
	
	//重写toSting方法
	public String toString() {
		StringBuffer results = new StringBuffer();
		Class clazz = getClass();
		results.append(clazz.getName() + "\n");
		Field[] fields = clazz.getDeclaredFields();
		try {
			AccessibleObject.setAccessible(fields, true);
			for (int i = 0; i < fields.length; i++) {
				Object obj = fields[i].get(this);
				if (((obj instanceof java.sql.Date))
						|| ((obj instanceof java.util.Date))) {
					java.util.Date dt = (java.util.Date) obj;
					results.append("\t"
							+ fields[i].getName()
							+ "="
							+ DateUtil.converToString(dt, "yyyy-MM-dd HH:mm:ss")
							+ "\n");
				} else {
					results.append("\t" + fields[i].getName() + "=" + fields[i].get(this) + "\n");
				}
			}
		} catch (Exception e) {
		}
		return results.toString();
	}
}
